/**
 * [49] 字母异位词分组
 *
 * @format
 * @lc app=leetcode.cn id=49 lang=javascript
 */

// @lc code=start
/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function (strs) {
    function stringSort(str) {
        let letters = str.split("")
        letters = letters.map(letter => letter.charCodeAt())
        letters.sort((a, b) => a - b)
        letters = letters.map(ascii => String.fromCharCode(ascii))
        return letters.join("")
    }
    const [groups, groupIndex] = [[], {}]
    let key = ""
    for (let str of strs) {
        key = stringSort(str)
        if (groupIndex[key] == undefined) {
            groups.push([])
            groupIndex[key] = groups.length - 1
            groups[groupIndex[key]].push(str)
        } else groups[groupIndex[key]].push(str)
    }
    return groups
}
// @lc code=end
